import WeexNativeModule from "../../../common/WeexNativeModule";
import DateUtil from "../../../common/util/DateUtil";
import StringUtils from "../../../common/util/StringUtils";
import JsonFilter from "../../../common/util/JsonFilter";
import ACApi from "../../../common/http/ACApi";

let pageView;
let pageImpl;
let loginUser;
let iscreating = false;

class EMNewCreateOutImpl {
    constructor(vue) {
        pageImpl = this;
        pageView = vue;
        pageImpl.initView();
    }

    initView() {
        WeexNativeModule.getLoginUser(function (res) {
            loginUser = JSON.parse(res.loginUser);
            // WeexNativeModule.alert(loginUser)
            pageView.paramAdd.orgName = loginUser.orgname
            pageView.paramAdd.orgCode = loginUser.orgid
            pageView.paramAdd.doctorId = loginUser.id
            pageView.paramAdd.doctor = loginUser.username
            WeexNativeModule.getParam(function (data) {
                // WeexNativeModule.alert(data)
                let isout = data.isout;
                let currentDate = DateUtil.formatTime(new Date(), "yyyy-MM-dd hh:mm");
                let currentDates = DateUtil.formatTime(new Date(), "yyyy-MM-dd hh:mm:ss");
                if (isout) {
                    pageView.isoutcreate = true
                    let initdata = JSON.parse(data.data);
                    let carNo = initdata.carNo;
                    if (carNo) {
                        pageView.headerTitle = "新建急救单（" + carNo + "）";
                    } else {
                        pageView.headerTitle = "新建急救单";
                    }
                    pageView.paramAdd.isHelp = 1
                    pageView.paramAdd.carNo = initdata.carNo
                    pageView.paramAdd.carId = initdata.id
                    pageView.paramAdd.callTime = currentDates;
                    pageView.showcallTime = currentDate;
                    pageView.paramAdd.isHelp = 1
                    pageView.paramAdd.helpCode = 1
                    pageView.helpCodeName = '本区域120'
                } else {
                    pageView.paramAdd.helpCode = ""
                    pageView.paramAdd.isHelp = ""
                    pageView.paramAdd.callTime = ""
                    pageView.isoutcreate = false
                    pageView.headerTitle = "新建急救单";
                }

                pageView.showattackTime = currentDate;
                pageView.paramAdd.attackTime = currentDates;

            })
        })
    }

    getIDCardData() {
        WeexNativeModule.getIdCardResult(function (message) {
            pageView.paramAdd.userName = message.name;
            pageView.paramAdd.gender = message.gender === '女' ? '1' : '0'
            pageView.paramAdd.idcard = message.idCardNum
            let age = new Date().getFullYear() - new Date(message.birth).getFullYear()
            pageView.paramAdd.age = age
        })
    }

    setHelpCode(code, name) {
        pageView.helpCodeSelect = false;
        pageView.paramAdd.helpCode = code;
        pageView.helpCodeName = name;
    }

    selectCallDateTime() {
        let defaultdate = pageView.paramAdd.callTime;
        if (StringUtils.isEmpty(defaultdate)) {
            defaultdate = DateUtil.formatTime(new Date(), "yyyy-MM-dd hh:mm:ss")
        }

        WeexNativeModule.selectDateTime(defaultdate, false, false, true, true, true, false, false, "", "", function (time) {
            // WeexNativeModule.alert(time);
            let datetime = time.data;
            pageView.paramAdd.callTime = datetime;
            pageView.showcallTime = DateUtil.formatTime(new Date(datetime), "yyyy-MM-dd hh:mm")
        })
    }

    seletctAttackTime() {
        let defaultdate = pageView.paramAdd.attackTime;
        if (StringUtils.isEmpty(defaultdate)) {
            defaultdate = DateUtil.formatTime(new Date(), "yyyy-MM-dd hh:mm:ss")
        }
        WeexNativeModule.selectDateTime(defaultdate, false, false, true, true, true, false, false, "", "", function (time) {
            // WeexNativeModule.alert(time);
            let datetime = time.data;
            pageView.paramAdd.attackTime = datetime;
            pageView.showattackTime = DateUtil.formatTime(new Date(datetime), "yyyy-MM-dd hh:mm")
        })
    }

    onInputHJAddress(event) {
        if (pageView.issetaddressing) {
            pageView.issetaddressing = false;
            return
        }
        let value = event.value;
        WeexNativeModule.searchAddress(value, function (res) {
            pageView.listData = [];
            let result = res.result;
            for (let item in result) {
                let value = result[item]
                pageView.listData.push(value)
            }
            if (pageView.listData.length > 0) {
                pageView.ishjaddressshow = true;
            } else {
                pageView.ishjaddressshow = false;
            }
        })
    }

    onInputFBAddress(event) {
        if (pageView.issetaddressing) {
            pageView.issetaddressing = false;
            return
        }
        let value = event.value;
        WeexNativeModule.searchAddress(value, function (res) {
            pageView.listData = [];
            let result = res.result;
            for (let item in result) {
                let value = result[item]
                pageView.listData.push(value)
            }
            if (pageView.listData.length > 0) {
                pageView.isfbaddressshow = true;
            } else {
                pageView.isfbaddressshow = false;
            }
        })
    }

    setHJAddress(item) {
        pageView.paramAdd.address = item.district + item.name;
        pageView.ishjaddressshow = false;
        pageView.issetaddressing = true;
    }

    setFBAddress(item) {
        pageView.paramAdd.attackAddress = item.district + item.name;
        pageView.isfbaddressshow = false;
        pageView.issetaddressing = true;
    }

    setNotSureMinute() {
        pageView.surenotminute = !pageView.surenotminute;
        pageView.paramAdd.isNullAttackDetailTime = !pageView.paramAdd.isNullAttackDetailTime
        if (pageView.surenotminute) {
            pageView.paramAdd.attackTime = DateUtil.formatTime(new Date(pageView.paramAdd.attackTime), "yyyy-MM-dd")
            pageView.showattackTime = DateUtil.formatTime(new Date(pageView.paramAdd.attackTime), "yyyy-MM-dd")
        }
    }

    saveData() {
        if (iscreating) {
            return
        }
        iscreating = true;
        if (StringUtils.isEmpty(pageView.paramAdd.userName)) {
            pageView.paramAdd.userName = "无名氏";
        }
        if (StringUtils.isEmpty(pageView.paramAdd.age)) {
            pageView.paramAdd.age = 0;
        }
        if (pageView.paramAdd.isNullAttackDetailTime === true) {
            pageView.paramAdd.isNullAttackDetailTime = '1'
        }
        if (pageView.paramAdd.isNullAttackDetailTime === false) {
            pageView.paramAdd.isNullAttackDetailTime = '0'
        }
        pageView.paramAdd = JsonFilter.filter(pageView.paramAdd)
        // WeexNativeModule.alert(pageView.paramAdd)
        WeexNativeModule.showProgressDialog("正在新建，请稍等")
        if (pageView.isoutcreate) {
            ACApi.createEMOutPage(pageView.paramAdd, {
                onSuccess: function (res) {
                    WeexNativeModule.dimissProgressDialog();
                    let data = JSON.parse(res);

                    if (data.statusCode === 'SUCCESS') {
                        let taskcode = data.result;
                        WeexNativeModule.openActivity({
                            url: "yixiu://emnewmainactivity",
                            params: {
                                taskcode: taskcode
                            }
                        })
                        pageImpl.finish();
                    } else {
                        WeexNativeModule.toast("新建失败，请稍后再试")
                        iscreating = false;
                    }

                },
                onError: function (err) {
                    WeexNativeModule.toast("新建失败，请稍后再试")
                    iscreating = false;
                    WeexNativeModule.dimissProgressDialog();
                }
            })
        } else {
            ACApi.createEMInPage(pageView.paramAdd, {
                onSuccess: function (res) {
                    WeexNativeModule.dimissProgressDialog();
                    let data = JSON.parse(res);

                    if (data.statusCode === 'SUCCESS') {
                        let taskcode = data.result;
                        WeexNativeModule.openActivity({
                            url: "yixiu://emnewmainactivity",
                            params: {
                                taskcode: taskcode
                            }
                        })
                        pageImpl.finish();
                    } else {
                        WeexNativeModule.toast("新建失败，请稍后再试")
                        iscreating = false;
                    }

                },
                onError: function (err) {
                    WeexNativeModule.toast("新建失败，请稍后再试")
                    iscreating = false;
                    WeexNativeModule.dimissProgressDialog();
                }
            })
        }
    }

    finish() {
        WeexNativeModule.finishPage();
    }
}

export default EMNewCreateOutImpl;